//
//  RCMerchantListCell.m
//  RocketProject
//
//  Created by chen jiedan on 13-6-23.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantListCell.h"
#import "RCMerchantsListDataModel.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"
#import <QuartzCore/QuartzCore.h>
#import "UIImageView+WebCache.h"
#import "NSString+NSString_Ex_.h"

#define kLineImageFile           @"image_line_type3.png"
#define kBGImageFile             @"image_discount_cell_bg.png"
#define kDefaultImageFile        @"image_default.png"
#define kCardImageFile           @"image_merchantList_card.png"
#define kTicketImageFile         @"image_merchantList_ticket.png"
#define kBenefigImageFile        @"image_merchantList_benefit.png"


#define kMerchantNameColor 0xFF454446
#define kMerchantNameSize  16
#define kMerchantPriceColor 0xFF989797
#define kMerchantPriceSize 14
#define kMerchantTagsColor 0xFF989797
#define kMerchantTagsSize  13


@implementation RCMerchantListCell{
    UILabel *_title;
    
     
    TTTAttributedLabel *_priceDes;
    UIImageView        *_priceImageView;
    TTTAttributedLabel *_price;
    TTTAttributedLabel *_tags;
    
    UIImageView        *_placeImageView;
    TTTAttributedLabel *_distance;
    UIImageView        *_merchantImageView;
    
    UIView             *_scoreView;
    
    UIImageView        *_cradImageView;
    UIImageView        *_ticketImageView;
    UIImageView        *_benefitImageView;
    
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        self.layer.contents = (id)bgImage.CGImage;
        self.backgroundColor = [UIColor clearColor];
        [self setUpCellView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void) setUpCellView
{
//    self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]]];
    UIView *bgColor = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
    [self.contentView addSubview:bgColor];
    [bgColor setBackgroundColor:[UIColor colorWithRed:246.0/255 green:244.0/255 blue:243.0/255 alpha:1]];
    [bgColor release];
    
    UIImageView *defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 14, 90, 70)];
    [defaultImageView setImage:[UIImage imageNamed:kDefaultImageFile]];
//    [defaultImageView setBackgroundColor:[UIColor yellowColor]];
    [self addSubview:defaultImageView];
    [defaultImageView release];
    
    _merchantImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 14, 90, 70)];
    _merchantImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_merchantImageView];
    [_merchantImageView release];
    
    _title = [[UILabel alloc] initWithFrame:CGRectMake(110, 19, 150, 16)];
    _title.font = [UIFont fontWithName:kFontNameHelvetica size:kMerchantNameSize];
    _title.backgroundColor = [UIColor clearColor];
    _title.textColor = [UIColor colorWithHex:kMerchantNameColor];
    [self.contentView addSubview:_title];
    [_title release];
    
    _cradImageView = [[UIImageView alloc]init];
    _cradImageView.backgroundColor = [UIColor clearColor];
    [_cradImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kCardImageFile ofType:Nil]]];
    [self addSubview:_cradImageView];
    [_cradImageView release];
    
    _ticketImageView = [[UIImageView alloc]init];
    _ticketImageView.backgroundColor = [UIColor clearColor];
    [_ticketImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kTicketImageFile ofType:Nil]]];
    [self addSubview:_ticketImageView];
    [_ticketImageView release];
    
    _benefitImageView = [[UIImageView alloc]init];
    _benefitImageView.backgroundColor = [UIColor clearColor];
    [_benefitImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBenefigImageFile ofType:Nil]]];
    [self addSubview:_benefitImageView];
    [_benefitImageView release];
    
    _scoreView = [[UIView alloc] initWithFrame:CGRectMake(110, 43, 80, 30)];
    [self.contentView addSubview:_scoreView];
    [_scoreView release];
    
    _priceDes = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(195, 45, 30, kMerchantPriceSize)];
    _priceDes.font = [UIFont fontWithName:kFontNameLightHeiti size:kMerchantPriceSize];
    _priceDes.backgroundColor = [UIColor clearColor];
    _priceDes.textColor = [UIColor colorWithHex:kMerchantNameColor];
    _priceDes.numberOfLines = 0;
    _priceDes.leading = 6;
    [self.contentView addSubview:_priceDes];
    [_priceDes release];
    
//    _priceImageView = [[UIImageView alloc] initWithFrame:CGRectMake(190, 15, img.size.width, img.size.height)];
//    _priceImageView.image = img;
//    [self.contentView addSubview:_priceImageView];
//    [_priceImageView release];
    
    
    _price = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(110, 45, 30, kMerchantPriceSize)];
    _price.font = [UIFont fontWithName:kFontNameLightHeiti size:kMerchantPriceSize];
    _price.backgroundColor = [UIColor clearColor];
    _price.textColor = [UIColor colorWithHex:kMerchantNameColor];
    _price.numberOfLines = 0;
    _price.leading = 6;
    [self.contentView addSubview:_price];
    [_price release];

    UIImage *img = [UIImage imageNamed:@"image_allCity_district"];    
    _placeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(250, 65, img.size.width, img.size.height)];
    _placeImageView.image = img;
    [self.contentView addSubview:_placeImageView];
    [_placeImageView release];
    
    _distance = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(250+img.size.width+5, 65, 70, kMerchantTagsSize)];
    _distance.font = [UIFont fontWithName:kFontNameHelvetica size:kMerchantTagsSize];
    _distance.backgroundColor = [UIColor clearColor];
    _distance.textColor = [UIColor colorWithHex:kMerchantTagsColor];
    _distance.numberOfLines = 0;
    _distance.leading = 6;
    [self.contentView addSubview:_distance];
    [_distance release];
    
    
    _tags = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(110, 65, 140, 15)];
    _tags.font = [UIFont fontWithName:kFontNameHelvetica size:kMerchantTagsSize];
    _tags.backgroundColor = [UIColor clearColor];
    _tags.textColor = [UIColor colorWithHex:kMerchantTagsColor];
    _tags.numberOfLines = 0;
    _tags.leading = 6;
    [self.contentView addSubview:_tags];
    [_tags release];

    


    UIImageView *_lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 89, 320, 1)];
    _lineImageView.backgroundColor = [UIColor clearColor];
    NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kLineImageFile];
    [_lineImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
    [self.contentView addSubview:_lineImageView];
    [_lineImageView release];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

- (void) bindData:(RCMerchant *)merchantData
{
    if (nil == merchantData) {
        return;
    }
    _title.text = merchantData.merchantName;
    
    CGFloat width = 110;
    CGSize titleSize = [_title.text sizeWithFont:_title.font constrainedToSize:CGSizeMake(2000, 20)];
    if (titleSize.width >150) {
        width += 150;
    }else{
        width +=titleSize.width;
    }
    
    width +=10;
    _cradImageView.hidden = YES;
    _ticketImageView.hidden = YES;
    _benefitImageView.hidden = YES;
    
    if (merchantData.isCard) {
        _cradImageView.hidden = NO;
        _cradImageView.frame = CGRectMake(width, 20, 14, 14);
        width +=16;
    }
    
    if (merchantData.isTicket) {
        _ticketImageView.hidden = NO;
        _ticketImageView.frame = CGRectMake(width, 20, 14, 14);
        width +=16;
    }
    
    if (merchantData.isBenefit) {
        _benefitImageView.hidden = NO;
        _benefitImageView.frame = CGRectMake(width, 20, 14, 14);
    }
    
    _priceDes.hidden = YES;
    _price.hidden = YES;
    if (![NSString stringIsNullOrEmpty:merchantData.merchantPrice]) {
        _priceDes.hidden = NO;
        _price.hidden = NO;
        _priceDes.text = @"人均:￥";
        _price.text = merchantData.merchantPrice;
    }
    
    
    
    for (UIView *subView in _scoreView.subviews) {
        [subView removeFromSuperview];
    }
    for (int i = 0; i < 5; i++) {
        UIImageView *t = [[UIImageView alloc] initWithFrame:CGRectMake(16*i, 0 ,15, 15)];
        if (merchantData.merchantScore > i) {
            [t setImage:[UIImage imageNamed:@"start_highlight"]];
        }else{
            [t setImage:[UIImage imageNamed:@"start_normal"]];
        }
        [_scoreView addSubview:t];
        [t release];
    }
    
    NSAttributedString *attributeStr = _priceDes.attributedText;
    CGSize size = [attributeStr sizeWithConstrainedToSize:CGSizeMake(279, 2000) Leading:6];
    
    CGRect priceDescFrame = _priceDes.frame;
    priceDescFrame.size = size;
    _priceDes.frame = priceDescFrame;
    
    CGRect priceImageFrame = CGRectMake(_priceDes.frame.origin.x+size.width, priceDescFrame.origin.y, _priceImageView.frame.size.width, _priceImageView.frame.size.height);
    _priceImageView.frame = priceImageFrame ;
    
    
    CGRect priceFrame = CGRectMake(priceImageFrame.origin.x+priceImageFrame.size.width, priceImageFrame.origin.y, _price.frame.size.width, _price.frame.size.height);
    _price.frame = priceFrame;
    
    NSMutableString *str = [[NSMutableString alloc] init];
    for (NSString *tag in merchantData.merchantTags) {
        [str appendFormat:@"%@ ",tag];
    }
    if ([str isEqualToString:@""]) {
        _tags.text = merchantData.merchantTagsString;
        
        NSString *distance = nil;
        if ([merchantData.merchantDistance floatValue] > 1000) {
            float distanceKm = [merchantData.merchantDistance floatValue]/1000.0;
            if ( ((int)distanceKm) > 999 )
                distance = [NSString stringWithFormat:@"%d+km",999];
            else
                distance = [NSString stringWithFormat:@"%.1fkm",distanceKm];
        }else{
            distance = [NSString stringWithFormat:@"%@m",merchantData.merchantDistance];
        }
        
        if ([merchantData.merchantDistance intValue] <= 0) {
            _placeImageView.hidden = _distance.hidden = YES;
        }else{
            _placeImageView.hidden = _distance.hidden = NO;
        }
        _distance.text = distance;

    }else{
        _tags.text = str;
        
        NSString *distance = nil;
        if ([merchantData.merchantDistance floatValue] > 1000) {
            float distanceKm = [merchantData.merchantDistance floatValue]/1000.0;
            if ( ((int)distanceKm) > 999 )
                distance = [NSString stringWithFormat:@"%d+Km",999];
            else
                distance = [NSString stringWithFormat:@"%.1fKm",distanceKm];
        }else{
            distance = [NSString stringWithFormat:@"%@m",merchantData.merchantDistance];
        }
        
        if ([merchantData.merchantDistance intValue] <= 0) {
            _placeImageView.hidden = _distance.hidden = YES;
        }else{
            _placeImageView.hidden = _distance.hidden = NO;
        }
        
        _distance.text = distance;

    }
    [str release];
    
    if (![NSString stringIsNullOrEmpty:merchantData.merchantImageUrl]) {
        [_merchantImageView setImageWithURL:[NSURL URLWithString:merchantData.merchantImageUrl] placeholderImage:[UIImage imageNamed:kDefaultImageFile]];
    }

    
    //    cellHeight += size.height +10;
}

@end
